﻿namespace SenGe.Application.Services
{
    /// <summary>
    /// 缓存接口
    /// 未做熔断和降级策略,可在业务服务中判断当缓存不可用时直接访问数据库
    /// </summary>
    public interface ICacheService
    {
        /// <summary>
        /// 在缓存中查找缓存对象
        /// </summary>
        /// <typeparam name="T">数据实体对象</typeparam>
        /// <param name="key">缓存键</param>
        /// <returns></returns>
        Task<T?> GetAsync<T>(string key);
        /// <summary>
        /// 设置数据缓存
        /// </summary>
        /// <typeparam name="T">数据实体对象</typeparam>
        /// <param name="key">缓存键</param>
        /// <param name="value">数据对象</param>
        /// <param name="expiration">过期时间</param>
        /// <returns></returns>
        Task SetAsync<T>(string key, T value, TimeSpan? expiration = null);
        /// <summary>
        /// 获取或新建缓存
        /// </summary>
        /// <typeparam name="T">数据实体对象</typeparam>
        /// <param name="key">缓存键</param>
        /// <param name="factory">数据工厂</param>
        /// <param name="expiration">过期时间</param>
        /// <returns></returns>
        Task<T?> GetOrCreateAsync<T>(string key, Func<Task<T>> factory, TimeSpan? expiration = null);
        /// <summary>
        /// 通过缓存键移除缓存对象
        /// </summary>
        /// <param name="key">缓存键</param>
        /// <returns></returns>
        Task RemoveAsync(string key);
        /// <summary>
        /// 判断指定缓存键是否已存在
        /// </summary>
        /// <param name="key">缓存键</param>
        /// <returns></returns>
        Task<bool> ExistsAsync(string key);
    }
}
